package com.jyb.qiyefuwu.view.activity.usercenter;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.databinding.ActivityApplyForPaymentBinding;
import com.jyb.qiyefuwu.entity.DepartmentEntity;
import com.jyb.qiyefuwu.eventmodel.UpdateSaleListEvent;
import com.jyb.qiyefuwu.viewmodel.ApplyForPaymentModel;
import com.sgb.link.constant.RequestCode;
import com.sgb.link.router.PathEnterpriseServices;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import static com.jyb.qiyefuwu.view.activity.usercenter.SalesOrderDetailsActivity.COPYRIGHT;
import static com.jyb.qiyefuwu.view.activity.usercenter.SalesOrderDetailsActivity.PATENT;
import static com.jyb.qiyefuwu.view.activity.usercenter.SalesOrderDetailsActivity.TRADE_MARK;


/**
 * 作者:陈方凯
 * 日期:2021/10/14
 * 说明:申请付款
 */
@Route(path = PathEnterpriseServices.ApplyForPaymentActivity)
public class ApplyForPaymentActivity extends BaseActivity<ApplyForPaymentModel, ActivityApplyForPaymentBinding> implements ItemInputTextView.OnWholeItemClickListener {
    private String oderId;
    private int zhiChanType;
    private String sellerName;
    private String sellerNo;
    private String price;
    private String createTime;
    private String id;
    private String companyId;
    private String companyName;
    private String publishType;
    private String agentNo;
    private String agent;
    private int position;

    @Override
    protected int layoutId() {
        return R.layout.activity_apply_for_payment;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(ApplyForPaymentModel.class);
    }

    @Override
    protected void init() {

        oderId = getIntent().getStringExtra("oderId");
        zhiChanType = getIntent().getIntExtra("zhiChanType", 110118);
        sellerName = getIntent().getStringExtra("sellerName");
        sellerNo = getIntent().getStringExtra("sellerNo");
        price = getIntent().getStringExtra("price");
        createTime = getIntent().getStringExtra("createTime");
        price = getIntent().getStringExtra("price");
        id = getIntent().getStringExtra("id");
        publishType = getIntent().getStringExtra("publishType");
        agentNo = getIntent().getStringExtra("agentNo");
        agent = getIntent().getStringExtra("agent");

        mBinding.backView.setOnClickListener(this);
        mBinding.tvCommit.setOnClickListener(this);
        mBinding.itemTvDepartment.setOnWholeItemClickListener(this);
        initObserver();
        initData();
    }

    private void initObserver() {
        //提示语
        mViewModel.dataText.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(ApplyForPaymentActivity.this, s);
                if (s.equals("申请提交失败")) {
                    EventBus.getDefault().post(new UpdateSaleListEvent(2, 1));
                    Intent mintent = new Intent();
                    setResult(RequestCode.CODE_ONE, mintent);
                    finish();
                }
                if("管理员已经关闭订单付款流程".equals(s)){
                    EventBus.getDefault().post(new UpdateSaleListEvent(2,1));
                    Intent mintent = new Intent();
                    mintent.putExtra("examinationId", mViewModel.examinationId.getValue());
                    setResult(RequestCode.CODE_ZERO, mintent);
                    finish();
                }
            }
        });
        //返回id
        mViewModel.examinationId.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                mViewModel.necessaryEntity.setAccount(companyName);
                mViewModel.necessaryEntity.setIsFeePayer("1");
                mViewModel.necessaryEntity.setMatters("企业服务");
                mViewModel.necessaryEntity.setMethod("在线付款");
                mViewModel.necessaryEntity.setOrderId(oderId);
                mViewModel.necessaryEntity.setOrderType("0006");
                mViewModel.necessaryEntity.setCreator(companyId);
                mViewModel.necessaryEntity.setMoney(price);

                if (publishType.equals("1")) {
                    //企业
                    mViewModel.necessaryEntity.setPayeeAccountName(sellerName);
                    mViewModel.necessaryEntity.setPayeeCompany(sellerNo);
                } else if (publishType.equals("0")) {
                    //个人
                    mViewModel.necessaryEntity.setPayeeName(agent);
                    mViewModel.necessaryEntity.setPayeeNo(agentNo);
                } else if (publishType.equals("2")) {
                    //团队
                    mViewModel.necessaryEntity.setPayeeName(agent);
                    mViewModel.necessaryEntity.setPayeeNo(agentNo);
                }

                mViewModel.necessaryEntity.setPayerAccountName(companyName);
                mViewModel.necessaryEntity.setPayerCompany(companyId);
                mViewModel.necessaryEntity.setTradeType("2");
                mViewModel.necessary(mViewModel.necessaryEntity);

            }
        });
        //部门
        mViewModel.departmentData.observe(this, new Observer<List<DepartmentEntity>>() {
            @Override
            public void onChanged(List<DepartmentEntity> departmentEntities) {
                if (departmentEntities.size() == 0) {
                    MToast.showToast(ApplyForPaymentActivity.this, "暂无数据");
                    return;
                }
                showDepPop();
            }
        });
        mViewModel.ShowLoading.observe(this, integer -> {
            switch (integer) {
                case 1:
                    DialogHelper.showProgressDialog(this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;

                case BaseViewModel.FINISH_AND_DISMISS_LODING:
                    DialogHelper.dismissProgressDialog();
                    MToast.showLongToast(this,"申请提交成功");
                    EventBus.getDefault().post(new UpdateSaleListEvent(2, 1));
                    Intent mintent = new Intent();
                    mintent.putExtra("examinationId", mViewModel.examinationId.getValue());
                    setResult(RequestCode.CODE_ZERO, mintent);
                    finish();
                    break;
            }
        });

    }

    private void initData() {
        //申请单位
        companyName = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NAME, "");
        //申请单位id
        companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        //申请人
        String applyPerson = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_NAME, "");
        //申请人id
        String applyPersonId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        //申请单位名称
        mViewModel.applyData.setCompanyName(companyName);
        //申请单位编号
        mViewModel.applyData.setCompanyId(companyId);
        //申请人名称
        mViewModel.applyData.setApplierName(applyPerson);
        //申请人id
        mViewModel.applyData.setUserId(applyPersonId);
        //订单id
        mViewModel.applyData.setObjId(oderId);
        //收款方
        mViewModel.applyData.setPayeeName(sellerName);
        //创建时间
        mViewModel.applyData.setCreateOrderTime(createTime);
        //订单编号为 +（）+的付款申请
        mViewModel.applyData.setBusinessName("订单编号为" + oderId + "的付款申请");
        //审批id
        mViewModel.applyData.setId(id);
        //审批类型
        mViewModel.applyData.setApplyType("enterpriseOrder");


        mBinding.itemTvCompany.setContent(companyName);
        mBinding.itemTvPerson.setContent(applyPerson);
        mBinding.itemTvOderNo.setContent(oderId);
        if (zhiChanType == TRADE_MARK) {
            mViewModel.applyData.setTransferType("商标转让");
            mBinding.itemTvType.setContent("商标转让");
        } else if (zhiChanType == PATENT) {
            mViewModel.applyData.setTransferType("专利转让");
            mBinding.itemTvType.setContent("专利转让");
        } else if (zhiChanType == COPYRIGHT) {
            mViewModel.applyData.setTransferType("版权转让");
            mBinding.itemTvType.setContent("版权转让");
        }
        mBinding.itemTvPayee.setContent(sellerName);
        mBinding.itemTvPrice.setContent("￥" + price);
        mViewModel.applyData.setTotalPrice(price);
    }

    private void checkData() {
        if (TextUtils.isEmpty(mBinding.tvMark.getEditContent())) {
            MToast.showToast(this, "请输入付款事项");
            return;
        }
        mViewModel.applyData.setRemark(mBinding.tvMark.getEditContent());
        mViewModel.applyForPayment(mViewModel.applyData);
    }

    /**
     * 部门弹窗
     */
    private void showDepPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(ApplyForPaymentActivity.this, (options1, options2, options3, v) -> {
            String content = mViewModel.departmentData.getValue().get(options1).getName();
            String id = mViewModel.departmentData.getValue().get(options1).getId();
            position = options1;
            mBinding.itemTvDepartment.setContent(content);
            mViewModel.applyData.setDepartmentName(content);
            mViewModel.applyData.setDepartmentNo(id);
        }).setCancelColor(getResources().getColor(R.color.color_666666)).
                setSubmitColor(getResources().getColor(R.color.new_blue))
                .build();
        businessOption.setSelectOptions(position);
        businessOption.setPicker(mViewModel.departmentData.getValue());
        businessOption.setTitleText("请选择部门");
        businessOption.show();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back_view) {
            finish();
        } else if (id == R.id.tv_commit) {
            checkData();
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        int id = view.getId();
        if (id == R.id.item_tv_department) {
            if (mViewModel.departmentData.getValue() == null) {
                mViewModel.applyForPayment(companyId);
            } else {
                showDepPop();
            }
        }
    }
}
